<template>
  <div class="type-nav">
    <div class="container">
      <div class="nav-left" @mouseleave="handleMouseLeave">
        <h2 class="all" @mouseenter="ShowMouseEnter">全部商品分类</h2>

        <!-- 1、谁在变给谁加内置组件transition 
             2、给transiton起name名字，那么是给类名起的前缀
             3、根据元素是进入还是离开有过度去写对应的类-->
        <transition name="sort">
          <div class="sort" v-show="isShowNav">
            <div class="all-sort-list2" @click="btnSearch">
              <div
                class="item"
                v-for="c1 in categoryList.slice(0, -2)"
                :key="c1.categoryId"
              >
                <h3>
                  <!-- 一级导航链接 -->
                  <a
                    data-level="1"
                    :data-id="c1.categoryId"
                    :data-name="c1.categoryName"
                    >{{ c1.categoryName }}</a
                  >
                </h3>
                <div class="item-list clearfix">
                  <div class="subitem">
                    <dl
                      class="fore"
                      v-for="c2 in c1.categoryChild"
                      :key="c2.categoryId"
                    >
                      <dt>
                        <!-- 二级导航链接 -->
                        <a
                          data-level="2"
                          :data-id="c2.categoryId"
                          :data-name="c2.categoryName"
                          >{{ c2.categoryName }}</a
                        >
                      </dt>
                      <dd>
                        <em v-for="c3 in c2.categoryChild" :key="c3.categoryId">
                          <!-- 三级导航链接 -->
                          <a
                            data-level="3"
                            :data-id="c3.categoryId"
                            :data-name="c3.categoryName"
                            >{{ c3.categoryName }}</a
                          >
                        </em>
                        <em>
                          <a href="">文学</a>
                        </em>
                        <em>
                          <a href="">经管</a>
                        </em>
                        <em>
                          <a href="">畅读VIP</a>
                        </em>
                      </dd>
                    </dl>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </transition>
      </div>
      <nav class="nav">
        <a href="###">服装城</a>
        <a href="###">美妆馆</a>
        <a href="###">尚品汇超市</a>
        <a href="###">全球购</a>
        <a href="###">闪购</a>
        <a href="###">团购</a>
        <a href="###">有趣</a>
        <a href="###">秒杀</a>
      </nav>
    </div>
  </div>
</template>

<script>
// import ajax from '../../../api/ajax'
// import {reqCategoryListData} from '../../../api/index'
import { mapState } from "vuex"; //引入mapState
export default {
  name: "TypeNav",
  data() {
    return {
      isShowNav: true,
    };
  },
  computed: {
    // 现在分模块了，所以使用mapState的时候，第一项应该指定模块名称
    ...mapState("home", ["categoryList"]),
  },
  methods: {
    btnSearch(event) {
      // this.$router.push('/search')
      // console.log(event);
      // console.log(event.target);
      // console.log(event.target.dataset);// 获取自定义属性中的数据

      //获取指定的数据
      let { level, id, name } = event.target.dataset;
      if (!level) return;

      const { keyword } = this.$route.query;
      this.$router.push({
        name: "search",
        query: {
          keyword,
          ["category" + level + "Id"]: id,
          categoryName: name,
        },
      });
    },

    //鼠标进入h2标签，显示TypeNav
    ShowMouseEnter() {
      this.isShowNav = true;
    },
    //鼠标离开nav-left，隐藏Search中的TypeNav，但是会导致Home页面的也消失，用路由元解决
    handleMouseLeave() {
      if (!this.$route.meta.isShowTypeNav) {
        this.isShowNav = false;
      }
    },
  },
  mounted() {
    //发送请求，获取对应的数据
    /* ajax.get('/api/product/getBaseCategoryList').then(result=>{
        console.log(result);
     },error=>{
        console.log(error.message);
     }) */

    /* reqCategoryListData().then(result=>{
        console.log(result);
     },error=>{
        console.log(error.message);
     }) */
    //在App.vue中发送请求，这样只需要请求一次就行，节省资源
    //this.$store.dispatch('home/getCategoryListData')

    //一开始要判断一下TypeNav是显示还是隐藏
    //console.log(this.$route);
    //下面这个判断是判断search页面是否显示TypeNav，使用路由元
    if (!this.$route.meta.isShowTypeNav) {
      this.isShowNav = false;
    }
  },
};
</script>

<style lang="less" scoped>
.type-nav {
  border-bottom: 2px solid #e1251b;

  .container {
    width: 1200px;
    margin: 0 auto;
    display: flex;
    position: relative;

    .all {
      width: 210px;
      height: 45px;
      background-color: #FF5571;
      line-height: 45px;
      text-align: center;
      color: #fff;
      font-size: 14px;
      font-weight: bold;
      border-bottom: 2px solid #e1251b;
    }

    .nav {
      a {
        height: 45px;
        margin: 0 22px;
        line-height: 45px;
        font-size: 16px;
        color: #333;
      }
    }

    .sort {
      position: absolute;
      left: 0;
      top: 48px;
      width: 210px;
      height: 461px;
      position: absolute;
      background:#FF5571 ;
      z-index: 999;
      border-top: 1px solid #FF5571;
      &.sort-enter{
         height: 0;
         opacity: 0;
      }
      &.sort-enter-to{
          height: 461px;
          opacity: 1;
      }
      &.sort-enter-active{
          transition: all 1s;
      }
      &.sort-leave{
         height: 461px;
          opacity: 1;
      }
      &.sort-leave-to{
         height: 0;
          opacity: 0;
      }
      &.sort-leave-active{
          transition: all 1s;
      }

      .all-sort-list2 {
        .item {
          h3 {
            line-height: 30px;
            font-size: 14px;
            font-weight: 400;
            overflow: hidden;
            padding: 0 20px;
            margin: 0;

            a {
              color: #fff;
            }
          }

          .item-list {
            display: none;
            position: absolute;
            width: 734px;
            min-height: 460px;
            background: #f7f7f7;
            left: 210px;
            border: 1px solid #ddd;
            top: 0;
            z-index: 9999 !important;

            .subitem {
              float: left;
              width: 650px;
              padding: 0 4px 0 8px;

              dl {
                border-top: 1px solid #eee;
                padding: 6px 0;
                overflow: hidden;
                zoom: 1;

                &.fore {
                  border-top: 0;
                }

                dt {
                  float: left;
                  width: 54px;
                  line-height: 22px;
                  text-align: right;
                  padding: 3px 6px 0 0;
                  font-weight: 700;
                }

                dd {
                  float: left;
                  width: 415px;
                  padding: 3px 0 0;
                  overflow: hidden;

                  em {
                    float: left;
                    height: 14px;
                    line-height: 14px;
                    padding: 0 8px;
                    margin-top: 5px;
                    border-left: 1px solid #ccc;
                  }
                }
              }
            }
          }

          &:hover {
            background-color: pink;
            cursor: pointer;

            .item-list {
              display: block;
            }
          }
        }
      }
    }
  }
}
</style>